package com.rxyl.love.petdoctor.app;

import android.app.Application;
import android.os.Environment;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.platform.comjni.map.commonmemcache.JNICommonMemCache;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.lidroid.xutils.HttpUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.util.SharedUtil;

import java.io.File;

/**
 * Created by Administrator on 2016/1/10 0010.
 */
public class MyApplication extends Application {

    private static MyApplication app;
    private HttpUtils httpUtils;
    private ImageLoader imageLoader = null;
    private DisplayImageOptions imageOptions;


    @Override
    public void onCreate() {
        super.onCreate();
        app = this;

        Fresco.initialize(getApplicationContext());
        SDKInitializer.initialize(getApplicationContext());

        initHttpUtils();
        initUniversalImageLoader();

        SharedUtil.init(getApplicationContext());

    }


    public static MyApplication getApp() {
        return app;
    }

    /**
     * 初始化HttpUtils
     */
    private void initHttpUtils() {
        httpUtils = new HttpUtils();

        httpUtils.configRequestThreadPoolSize(5);

        httpUtils.configRequestRetryCount(3);

        httpUtils.configResponseTextCharset("utf-8");

        httpUtils.configSoTimeout(30 * 1000);

        httpUtils.configTimeout(60 * 1000);
    }

    public HttpUtils getHttpUtils() {
        return this.httpUtils;
    }


    private void initUniversalImageLoader() {
        imageLoader = ImageLoader.getInstance();
        //内存大小
        int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 8);
        String cachePath = null;
        //磁盘缓存路径
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            cachePath = getApplicationContext().getExternalCacheDir().getPath();
        } else {
            cachePath = getApplicationContext().getCacheDir().getPath();
        }
        File cacheFileDir = new File(cachePath, "/petdoctor/images");
        cacheFileDir.mkdirs();

        //UniversalImageLoader的配置
        ImageLoaderConfiguration configuration =
                new ImageLoaderConfiguration.Builder(getApplicationContext())
                        .memoryCacheSize(cacheSize)
                        .diskCache(new UnlimitedDiskCache(cacheFileDir))
                        .diskCacheFileCount(300)
                        .diskCacheSize(200 * 1024 * 1024)
                        .build();
        ImageLoader.getInstance().init(configuration);

        //全局图片选项
        imageOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .build();
    }

    public ImageLoader getImageLoader() {
        return imageLoader;
    }

    public DisplayImageOptions getImageOptions() {
        return imageOptions;
    }



}
